package com.zyh.system.JDsupermarkt.service.impl;

import com.zyh.system.JDsupermarkt.dao.impl.BuyCarDaoImpl;
import com.zyh.system.JDsupermarkt.dao.impl.GoodsDaoImpl;
import com.zyh.system.JDsupermarkt.domain.BuyCar;
import com.zyh.system.JDsupermarkt.domain.Goods;
import com.zyh.system.JDsupermarkt.service.BuyCarService;

import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/2 20:21
 * @Description BuyCarService 实现类
 */

public class BuyCarServiceImpl implements BuyCarService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final BuyCarDaoImpl BUY_CAR_DAO = new BuyCarDaoImpl();
    public static final LinkedHashMap<Integer, BuyCar> BUY_CAR = BuyCarDaoImpl.BUY_CAR;
    public static final GoodsDaoImpl GOODS_DAO = new GoodsDaoImpl();

    /**
     * 添加购物车
     *
     * @param map Goods集合
     */
    @Override
    public void addBuyCar(LinkedHashMap<Integer, Goods> map) {
        System.out.println("请输入商品编号：");
        Integer goodsId = INPUT.nextInt();
        Goods buyCarInfo = GOODS_DAO.selectById(map, goodsId);
        if (buyCarInfo != null) {
            //假设库存为0
            boolean flag = false;
            Set<Integer> keySet = map.keySet();
            for (Integer key : keySet) {
                if (key.equals(goodsId)) {
                    Goods value = map.get(key);
                    if (value.getStock() != 0) {
                        flag = true;
                        break;
                    }
                }
            }
            if (flag) {
                Integer buyNum;
                Goods goods = GOODS_DAO.selectById(map, goodsId);
                while (true) {
                    System.out.println("请输入要购买的数量：");
                    buyNum = INPUT.nextInt();
                    if (buyNum < 0) {
                        System.out.println("购买数量不能小于0");
                        continue;
                    } else if (buyNum > goods.getStock()) {
                        System.out.println("购买数量不能大于" + goods.getStock());
                        continue;
                    } else {
                        //如果购物车之前有同编号商品
                        //遍历商品集合，找到集合中与输入的goodsId相同的商品
                        Set<Integer> keySet1 = map.keySet();
                        for (Integer key : keySet1) {
                            Goods value = map.get(key);
                            if (key.equals(goodsId)) {
                                //减少库存
                                value.setStock(value.getStock() - buyNum);
                                //遍历购物车集合，查询是否有同编号商品
                                boolean is_exist = false;
                                Set<Integer> buyCayKeys = BUY_CAR.keySet();
                                for (Integer buyCayKey : buyCayKeys) {
                                    if (buyCayKey.equals(goodsId)) {
                                        is_exist = true;
                                        break;
                                    }
                                }
                                if (is_exist) {
                                    //存在相同商品，购买数量累加
                                    for (Integer buyCayKey : buyCayKeys) {
                                        BuyCar buyCarValue = BUY_CAR.get(buyCayKey);
                                        if (buyCayKey.equals(goodsId)) {
                                            buyCarValue.setBuyNum(buyCarValue.getBuyNum() + buyNum);
                                            buyCarValue.setMoney(buyCarValue.getBuyNum() * buyCarValue.getGoodsPrice());
                                            System.out.println("添加成功");
                                            return;
                                        }
                                    }
                                } else {
                                    //不存在相同就创建新的购物车对象，存入购物车集合中
                                    BuyCar buyCar = new BuyCar(value.getGoodsName(), value.getGoodsPrice(), buyNum, (value.getGoodsPrice() * buyNum));
                                    BUY_CAR_DAO.addBuyCar(BUY_CAR, buyCar, goodsId);
                                    System.out.println("添加成功");
                                    return;
                                }
                            }
                        }
                    }
                }
            } else {
                System.out.println("缺货，等待补货");
            }
        } else {
            System.out.println("您输入的商品编号不存在！");
        }
    }

    /**
     * 展示购物车
     *
     * @param map BuyCar集合
     */
    @Override
    public void showBuyCay(LinkedHashMap<Integer, BuyCar> map) {
        System.out.println("编号\t商品名称\t\t\t商品价格\t数量\t小计");
        Double money = BUY_CAR_DAO.showBuyCay(map);
        if (map.isEmpty()) {
            System.out.println("购物车为空");
            return;
        }
        System.out.println("是否付款y/n");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            while (true) {
                System.out.println("请输入付款金额：");
                Double payMoney = INPUT.nextDouble();
                if (payMoney < money) {
                    System.out.println("金额不足！");
                    continue;
                } else {
                    System.out.println("付款成功！");
                    System.out.println("找零：" + (payMoney - money) + "元");
                    BUY_CAR_DAO.clearBuyCar(map);
                    return;
                }
            }
        }
    }

    /**
     * 根据id删除购物车的信息
     *
     * @param map
     */
    @Override
    public void deleteById(LinkedHashMap<Integer, BuyCar> map) {
        System.out.println("请输入要删除的购物车商品编号：");
        int delId = INPUT.nextInt();
        BuyCar buyCar = BUY_CAR_DAO.selectById(map, delId);
        if (buyCar == null) {
            System.out.println("您输入的编号不存在");
        } else {
            System.out.println("是否删除y/n");
            String confirm = INPUT.next();
            if ("y".equals(confirm)) {
                BuyCar removeBuyCar = BUY_CAR_DAO.deleteById(map, delId);
                Goods goods = GOODS_DAO.selectById(GoodsDaoImpl.GOODS_MAP, delId);
                goods.setStock(goods.getStock()+removeBuyCar.getBuyNum());
                System.out.println("删除成功");
            }
        }
    }
}